package com.sc.services.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.hikvision.artemis.sdk.config.ArtemisConfig;
import com.sc.entity.dto.HKData;
import com.sc.entity.dto.HKDetail;
import com.sc.entity.dto.ResponseDto;
import com.sc.entity.params.DeviceParam;
import com.sc.entity.params.MaterialParam;
import com.sc.services.HKCameraService;
import com.sc.utils.HKUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.sc.entity.dto.MaterialData;
import com.sc.entity.dto.MaterialDetail;
import com.sc.entity.dto.MaterialSingleData;
import com.sc.entity.dto.MaterialSingleDetail;

import java.util.*;

@Service
@Slf4j
public class HKCameraServiceImpl implements HKCameraService {
    @Override
    public HKData getDeviceList(DeviceParam param) {
        ArtemisConfig.appKey = "29925388";
        ArtemisConfig.appSecret = "VZKBp8e3Mo3ljtzqM7Hs";
        ArtemisConfig.host = "10.94.21.21:443";

        try {
            // TEST
       //     String response = HKUtils.pageList(param.getPageNo(), param.getPageSize());
            String response = HKUtils.getStatus(new ArrayList<>(), param.getPageNo(), param.getPageSize());
            if (StringUtils.isEmpty(response)) {
                return null;
            }
            ObjectMapper mapper = new ObjectMapper();
            var body = mapper.readValue(response, ResponseDto.class);

            if (body == null || !Objects.equals(body.getCode(), "0") || body.getData() == null) {
                log.info("Error get response from platform {}", response);
                return  null;
            }

            var data = body.getData();
            if (data instanceof LinkedHashMap<?, ?> hkData) {
                LinkedHashMap<String, Object> dataMap = (LinkedHashMap<String, Object>)hkData;
                HKData result = new HKData();
                result.setTotal(Integer.parseInt(String.valueOf(dataMap.get("total"))));
                var dataList = dataMap.get("list");

                if (dataList instanceof List<?> list) {
                    List<HKDetail> details = new ArrayList<>();
                    for (var item : list) {
                        if (item instanceof LinkedHashMap<?, ?> map) {
                            var itemMap = (Map<String, Object>) item;

                            HKDetail detail = new HKDetail();
                            BeanUtils.populate(detail, itemMap);

                            details.add(detail);
                        }
                    }

                    result.setList(details);
                }

                return result;
            }
            return null;
        } catch (Exception e) {
            return null;
        }
    }

    @Override
    public MaterialData getMaterialList(MaterialParam param) {
        ArtemisConfig.appKey = "29925388";
        ArtemisConfig.appSecret = "VZKBp8e3Mo3ljtzqM7Hs";
        ArtemisConfig.host = "10.94.21.21:443";
        try {
            String response = HKUtils.getMaterials(param);

            if (StringUtils.isEmpty(response)) {
                return null;
            }
            ObjectMapper mapper = new ObjectMapper();
            var body = mapper.readValue(response, ResponseDto.class);

            if (body == null || !Objects.equals(body.getCode(), "0") || body.getData() == null) {
                log.info("Error get response from platform {}", response);
                return  null;
            }

            var data = body.getData();
            if (data instanceof LinkedHashMap<?, ?> hkData) {
                LinkedHashMap<String, Object> dataMap = (LinkedHashMap<String, Object>)hkData;
                MaterialData result = new MaterialData();
                result.setTotal(Integer.parseInt(String.valueOf(dataMap.get("total"))));
                var dataList = dataMap.get("list");

                if (dataList instanceof List<?> list) {
                    List<MaterialDetail> details = new ArrayList<>();
                    for (var item : list) {
                        if (item instanceof LinkedHashMap<?, ?> map) {
                            var itemMap = (Map<String, Object>) map;

                            MaterialDetail detail = new MaterialDetail();
                            BeanUtils.populate(detail, itemMap);

                            details.add(detail);
                        }
                    }

                    result.setList(details);
                }

                return result;
            }
            return null;
        } catch (Exception e) {
            return null;
        }
    }

    @Override
    public MaterialSingleData getMaterialSingleDetail(Integer id) {
        ArtemisConfig.appKey = "29925388";
        ArtemisConfig.appSecret = "VZKBp8e3Mo3ljtzqM7Hs";
        ArtemisConfig.host = "10.94.21.21:443";
        try {
            String response = HKUtils.getMaterialSingleDetail(id);

            if (StringUtils.isEmpty(response)) {
                return null;
            }
            ObjectMapper mapper = new ObjectMapper();
            var body = mapper.readValue(response, ResponseDto.class);

            if (body == null || !Objects.equals(body.getCode(), "0") || body.getData() == null) {
                log.info("Error get response from platform {}", response);
                return  null;
            }

            var data = body.getData();
            if (data instanceof LinkedHashMap<?, ?> hkData) {
                LinkedHashMap<String, Object> dataMap = (LinkedHashMap<String, Object>)hkData;
                MaterialSingleData result = new MaterialSingleData();
                result.setTotal(Integer.parseInt(String.valueOf(dataMap.get("total"))));
                var dataList = dataMap.get("list");

                if (dataList instanceof List<?> list) {
                    List<MaterialSingleDetail> details = new ArrayList<>();
                    for (var item : list) {
                        if (item instanceof LinkedHashMap<?, ?> map) {
                            var itemMap = (Map<String, Object>) map;

                            MaterialSingleDetail detail = new MaterialSingleDetail();
                            BeanUtils.populate(detail, itemMap);

                            details.add(detail);
                        }
                    }

                    result.setList(details);
                }

                return result;
            }
            return null;
        } catch (Exception e) {
            return null;
        }
    }
}
